package com.zk.babala.datasource;

import com.zk.babala.entity.CategoryInfo;
import com.zk.babala.entity.LineInfo;
import com.zk.babala.entity.Movie;
import com.zk.babala.entity.PlayNumInfo;
import com.zk.babala.entity.VideoInfo;
import com.zk.babala.service.ISourceParse;
import com.zk.babala.util.LineUtil;
import com.zk.babala.util.RequestUtil;

import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: zhangke
 * @Date: 2022/4/11 17:20
 */
public class HGDJSource implements ISourceParse {
    private String nextUrl;

    @Override
    public Map<String, String> getSourceMeta() {
        Map<String, String> map = new HashMap<>();
        map.put("source", this.getClass().getSimpleName());
        map.put("name", "红果短剧");
        map.put("url", "https://www.hgdj.app/");
        return map;
    }

    public HGDJSource() {
    }

    @Override
    public List<CategoryInfo> getCategory() {
        String line = LineUtil.getLineUtil().getLine();
        List<CategoryInfo> categoryInfoList = new ArrayList<>();
        categoryInfoList.add(new CategoryInfo("热播", line));
        CategoryInfo dj = new CategoryInfo("短剧", line + "vodtype/duanju.html");
        dj.getChildList().add(new CategoryInfo("古装仙侠", line + "vodshow/guzhuangxianxia-----------.html"));
        dj.getChildList().add(new CategoryInfo("反转爽剧", line + "vodshow/fanzhuanshuangju-----------.html"));
        dj.getChildList().add(new CategoryInfo("年代穿越", line + "vodshow/niandaichuanyue-----------.html"));
        dj.getChildList().add(new CategoryInfo("脑洞悬疑", line + "vodshow/naodongxuanyi-----------.html"));
        dj.getChildList().add(new CategoryInfo("现代都市", line + "vodshow/xiandaidushi-----------.html"));
        dj.getChildList().add(new CategoryInfo("女频恋爱", line + "vodshow/nvpinlianai-----------.html"));
        categoryInfoList.add(dj);
        CategoryInfo dm = new CategoryInfo("动漫", line + "vodtype/dongman.html");
        dm.getChildList().add(new CategoryInfo("国产动漫", line + "vodshow/guochandongman-----------.html"));
        dm.getChildList().add(new CategoryInfo("日韩动漫", line + "vodshow/rihandongman-----------.html"));
        dm.getChildList().add(new CategoryInfo("欧美动漫", line + "vodshow/oumeidongman-----------.html"));
        categoryInfoList.add(dm);
        categoryInfoList.add(new CategoryInfo("电影", line + "vodtype/dianying.html"));
        return categoryInfoList;
    }

    @Override
    public List<Movie> getMovies(String url) {
        List<Movie> list = new ArrayList<>();
        Document document = RequestUtil.get(url);
        document.getElementsByClass("hl-item-thumb").forEach(a -> {
            if (!a.attr("title").isEmpty()) {
                Movie m = new Movie();
                m.setTitle(a.attr("title"));
                m.setVideoUrl(LineUtil.getLineUtil().swapUrl(a.attr("href")));
                m.setCardImageUrl(LineUtil.getLineUtil().swapUrl(a.attr("data-original")));
                list.add(m);
            }

        });

        nextUrl = null;
        Elements pageElement = document.getElementsByClass("hl-page-wrap");
        if (!pageElement.isEmpty()) {
            pageElement.get(0).getElementsByTag("a").forEach(page -> {
                if (page.text().equals("下一页")) {
                    nextUrl = page.attr("href");
                }
            });

        }
        return list;
    }

    @Override
    public VideoInfo getVideoDetail(String url) {
        VideoInfo videoInfo = new VideoInfo();
        Document document = RequestUtil.get(url);
        Elements infoNodes = document.getElementsByClass("hl-full-box").get(0).getElementsByTag("li");
        infoNodes.forEach(infoNode -> {
            videoInfo.getDetailInfoList().add(infoNode.text());
        });

        List<LineInfo> lineInfoList = new ArrayList<>();
        Elements playNode = document.getElementsByClass("hl-tabs-btn");
        Elements playListData = document.getElementsByClass("hl-plays-list");
        for (int i = 0; i < playNode.size(); i++) {
            Element node = playNode.get(i);
            List<PlayNumInfo> playItemList = new ArrayList<>();
            Elements playList = playListData.get(i).getElementsByTag("li");
            for (int p = 0; p < playList.size(); p++) {
                Element item = playList.get(p).getElementsByTag("a").get(0);
                PlayNumInfo play = new PlayNumInfo();
                play.setId(p);
                play.setTitle(item.text());
                play.setPageUrl(item.attr("href"));
                playItemList.add(play);
            }

            String lineTitle = node.text();
            LineInfo lineInfo = new LineInfo();
            lineInfo.setTitle(lineTitle);
            lineInfo.setPlayNumInfoList(playItemList);
            lineInfoList.add(lineInfo);
        }

        videoInfo.setLineInfoList(lineInfoList);
        return videoInfo;
    }

    @Override
    public String getPlayUrl(String url) {
        Document document = RequestUtil.get(url);
        String content = document.getElementsByClass("hl-player-wrap").get(0).toString();
        return LineUtil.getLineUtil().getPlayUrl(content);
    }

    @Override
    public String getSearchUrl() {
        return LineUtil.getLineUtil().getLine() + "vodsearch/-------------.html?wd=";
    }


    @Override
    public List<Movie> search(String word) {
        String searchUrl = getSearchUrl() + word;
        nextUrl = null;
        return getMovies(searchUrl);
    }

    @Override
    public List<Movie> loadNext() {
        if (nextUrl == null) {
            return Collections.emptyList();
        } else {
            return getMovies(nextUrl);
        }
    }


}
